package cn.org.mrliu.ssm.common.web.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

/**
 * 系统异常处理器
 * @author MrLiu
 *
 */
public class SystemMappingExceptionResolver extends SimpleMappingExceptionResolver {
	private static Logger log = Logger.getLogger(SystemMappingExceptionResolver.class);
	@Override
	protected ModelAndView doResolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		isAjax(request);
		log.debug("------------------------------------ Exception ------------------------------------");
		log.error(ex.getMessage(), ex);
		log.debug("-----------------------------------------------------------------------------------");
		return super.doResolveException(request, response, handler, ex);
	}

	/**
	 * 判断ajax请求
	 * @param request
	 * @return
	 */
	boolean isAjax(HttpServletRequest request){
		System.out.println(request.getHeader("X-Requested-With"));
	    return  (request.getHeader("X-Requested-With") != null  && "XMLHttpRequest".equals( request.getHeader("X-Requested-With").toString())   ) ;
	}
	
}
